package severlet;

import bean.Laterecord;
import dao.LaterecordDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "AdminLaterecordButtonServlet", value = "/AdminLaterecordButtonServlet")
public class AdminLaterecordButtonServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        //判断是哪个按钮
        String button = request.getParameter("submit");
        String studentname = request.getParameter("studentname");
        String dormitoryid = request.getParameter("dormitoryid");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date latetime=null;
        try {
            latetime=sdf.parse(request.getParameter("latetime"));
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("=============输入时间有误==============");
            request.getRequestDispatcher("FanHui.jsp").forward(request,response);
        }
        String reason = request.getParameter("reason");

        //封装
        Laterecord laterecord=new Laterecord();
        laterecord.setStudentname(studentname);
        laterecord.setDormitoryid(dormitoryid);
        laterecord.setLatetime(latetime);
        laterecord.setReason(reason);

        //调用dao层
        LaterecordDao laterecordDao=new LaterecordDao();
        //判断按钮调用不同功能
        if (button.equals("submit")){
            laterecordDao.submitInfo(laterecord);
        }else if (button.equals("modify")){
            laterecordDao.changeInfoByDormidAndStuname(laterecord);
        }
        //请求转发  去查询页面
        request.getRequestDispatcher("/AdminLaterecordServlet").forward(request,response);
    }
}
